'''
Created on 2014-5-24

@author: ASUS
'''
from cmath import pi
from __builtin__ import file, cmp
from read_list_from_file.read_guess_list import read_guess
from fetch_info_to_file.write_guess import write_guess_to_file
from distutils.log import info

class Guess():
    def get_purchase_info(self):
        name=self.get_all_consumers()
        info=self.get_all_review_stuff()
        purchase_info=[]
        for single in name:
            asins=[]
            times=[]
            for each in info:
                for single_review in each['single_review']:
                    if single==single_review['consumer']:
                        asins.append(each['ASIN'])
                        times.append(single_review['time'])
                    else:
                        continue
            purchase={'consumer':single,'ASINS':asins,'time':times}
            purchase_info.append(purchase) 
        write_guess_to_file(r'../txt_package/purchase.txt',purchase_info)        
        return purchase_info
        
        
    def get_all_consumers(self):
        file=read_guess(r'../txt_package/useful_reviews_2.txt')
        name=[]
        for line in file:
            for single in line['single_review']:
                if single['consumer'] in name:
                    continue
                else:
                    name.append(single['consumer'])
        write_guess_to_file(r'../txt_package/consumers.txt', name)
        return name

    def get_all_review_stuff(self):
        info=read_guess(r'../txt_package/useful_reviews_2.txt')
        return info
    
    
    
    def StoreGuess(self):
        info=self.get_all_review_stuff()
        purchase=read_guess(r'../txt_package/purchase.txt')
        guess_info=[]
        customer=[]
        for one in purchase:
            customer.append(one['consumer'])
        for single in info[21:40]:
            asin=single['ASIN']
            reviews=single['single_review']
            temp=[]
            for each in reviews:
                pos=reviews.index(each)
                for i in reviews[pos:]:
                    index=customer.index(i['consumer'])
                    asins=purchase[index]['ASINS']
                    for a in asins:
                        ASIN_ASIN=asin+'-'+a
                        distance=self.calTasteValue(i['rank'], each['rank'])*self.calTimeValue(purchase[index]['time'][asins.index(a)], each['time'])
                        ASIN_distance={'ASIN-ASIN':ASIN_ASIN,'distance':distance}
                        temp.append(ASIN_distance)
                        guess_info.append(ASIN_distance)
        write_guess_to_file(r'../txt_package/guess_2.txt', guess_info)
        print guess_info               
                    
                     
    
    def calTasteValue(self,rank_1,rank_2):
        if rank_1=='':
            rank_1=0
        if rank_2=='':
            rank_2=0
        return 6-abs(int(rank_1)-int(rank_2))
    
    def calTimeValue(self,time_1,time_2):
        dates=abs(int(time_1['year'])-int(time_2['year']))+abs(int(time_1['month'])-int(time_2['month']))
        +abs(int(time_1['day'])-int(time_2['day']))
        distance=int(365/(dates*10*pi+1))
        return distance
    
    def dealWithResult(self):
        ASIN_distance=read_guess(r'../txt_package/guess_2.txt')
        info=[]
        for one in ASIN_distance:
            is_in=False
            for single in info:
                if one['ASIN-ASIN']==single['ASIN-ASIN']:
                    is_in=True
                    break
                else:
                    continue
            if is_in:
                continue
            else:
                index=ASIN_distance.index(one)
                distance=one['distance']
                for each in ASIN_distance[index:]:
                    if each['ASIN-ASIN']==one['ASIN-ASIN']:
                        distance=distance+each['distance']
                ASIN_D={'ASIN-ASIN':one['ASIN-ASIN'],'Distance':distance}
                info.append(ASIN_D)
        write_guess_to_file(r'../txt_package/guess.txt', info)   
                 
    def buyAndGuess(self,ASIN):
        info=read_guess(r'../txt_package/guess_result.txt') 
        guess=[]
        asin=[]
        for each in info:   
            asin.append(each['ASIN'])
        if ASIN in asin:
            for one in info[asin.index(ASIN)]['distances']:
                guess.append(one['guess_ASIN'])
            return guess
        else:
            return 'no'
        
    
    
def getIntervalguess():
    G=Guess()
    info=G.read('guess')
    guess_like=[]
    for one in info:
        is_in=False
        for each in guess_like:
            if one['ASIN-ASIN'].split('-')[0]==each['ASIN']:
                is_in=True
                break
        if is_in:
            continue
        else:
            index=info.index(one)
            count=0
            one_guess=[]
            for single in info[index:]:
                if single['ASIN-ASIN'].split('-')[0]!=one['ASIN-ASIN'].split('-')[0]:
                    break
                else:
                    if single['ASIN-ASIN'].split('-')[1]==one['ASIN-ASIN'].split('-')[0]:
                        continue
                    else:
                        count=count+1
                        one_distance={'guess_ASIN':single['ASIN-ASIN'].split('-')[1],'distance':single['Distance']}
                        one_guess.append(one_distance)
            i={'ASIN':one['ASIN-ASIN'].split('-')[0],'distances':one_guess,'count':count}
            guess_like.append(i)
    write_guess_to_file(r'../txt_package/guess_3.txt', guess_like)
def compare(x,y):
    return cmp(int(x['distance']),int(y['distance']))
    
if __name__ == '__main__':
    G=Guess()
    print G.buyAndGuess('B00D386JBA') 
               